using DOTS.DOD.LESSON0;
using Unity.Burst;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Profiling;
using Unity.Transforms;

namespace DOTS.DOD.LESSON2
{
    [BurstCompile]
    partial struct RotateCubeWithIJobEntity : IJobEntity
    {
        public float DeltaTime;

        void Execute(ref LocalTransform transform, in RotateSpeed speed)
        {
            float v = 0f;
            for (int i = 0; i < 10000; i++)
            {
                float a = math.sqrt(speed.Value) + math.sin(i * i);
                v = a % float.MaxValue;
            }

            transform = transform.RotateY(v * DeltaTime);
        }
    }

    [BurstCompile]
    [UpdateInGroup(typeof(CubeRotateWithIJobEntitySystemGroup))]
    partial struct CubeRotateWithIJobEntitySystem : ISystem
    {
        static readonly ProfilerMarker mProfilerMaker =
            new ProfilerMarker("CubeRotateWithIJobEntitySystem UpdateTransform");

        [BurstCompile]
        public void OnUpdate(ref SystemState state)
        {
            using (mProfilerMaker.Auto())
            {
                var job = new RotateCubeWithIJobEntity()
                {
                    DeltaTime = SystemAPI.Time.DeltaTime,
                };
                //job.Schedule(); //单线程
                job.ScheduleParallel(); //多线程
            }
        }
    }
}